package com.ywwxhz.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.view.View;

import com.ywwxhz.Util.Config;
import com.ywwxhz.plugins.eyecare.EyesCareModel;
import com.ywwxhz.service.CoreService;
import com.ywwxhz.taskmgr.R;

public class LaboratoryPreferenceFragment extends BasePreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_laboratory);
    }

    @Override
    protected int setSubtitle() {
        return R.string.pref_laboratory;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findPreference(getString(R.string.pref_eyecare_key)).setOnPreferenceChangeListener(this);
    }


    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference.getKey().equals(getString(R.string.pref_eyecare_key))) {
            Intent autoKillService = new Intent(getActivity(), CoreService.class);
            autoKillService.putExtra(Config.SERVICE_PARAM, EyesCareModel.MODEL);
            if ((Boolean) newValue) {
                autoKillService.putExtra(EyesCareModel.PARAM,
                        EyesCareModel.VIEW_ENABLE);
            } else {
                autoKillService.putExtra(EyesCareModel.PARAM,
                        EyesCareModel.VIEW_DISABLE);
            }
            getActivity().startService(autoKillService);
        } else {
            return false;
        }
        return true;
    }

    @Override
    public boolean getShouldExit() {
        return true;
    }
}